<script setup>
import { showSuccessToast, showFailToast } from 'vant'
import { ref } from 'vue'
import { sendCode } from '@/api/login'
import { useRouter } from 'vue-router'
import useUserStore from '@/stores/modules/user'
import {getSessionStorageItem} from "@/utils/common.js";

const router = useRouter()
const userStore = useUserStore()

const form = ref({
  phone: undefined,
  code: undefined,
})
const sendCodeBtnDisabled = ref(false)
const sendCodeBtnTxt = ref('发送验证码')
const countdownTime = ref(60)
const countdownFlag = ref(0)
const submitLoading = ref(false)
// const data = Vue.reactive({
//   form: {
//     phone: undefined,
//     code: undefined,
//   },
//   sendCodeBtnDisabled: false,
//   sendCodeBtnTxt: '发送验证码',
//   countdownTime: 60,
//   countdownFlag: 0,
//   submitLoading: false,
// })
const clearCountdownFlag = () => {
  if (!!countdownFlag.value && countdownFlag.value > 0) {
    clearTimeout(countdownFlag.value)
  }
}
const resetCountdown = () => {
  countdownTime.value = 60
  sendCodeBtnDisabled.value = false
  sendCodeBtnTxt.value = '获取验证码'
  clearCountdownFlag()
}
const countdown = () => {
  if (countdownTime.value <= 0) {
    resetCountdown()
    return
  }
  sendCodeBtnTxt.value = countdownTime.value + '秒后重新获取'
  countdownTime.value--
  sessionStorage.setItem('countdownTime', countdownTime.value)
  setTimeout(countdown, 1000)
}
const startCountdown = () => {
  countdownFlag.value = setTimeout(countdown, 1000)
  sendCodeBtnDisabled.value = true
}
const getCode = () => {
  if(!form.value.phone || form.value.phone.length !== 11 || !(/^1[3456789]\d{9}$/.test(form.value.phone))) {
    showFailToast('手机号格式错误')
    return
  }
  startCountdown()
  sendCode(form.value.phone).then(response => {
    showSuccessToast({message: "验证码发送成功"});
  })
}

const onSubmit = () => {
  if(!form.value.phone || form.value.phone.length !== 11 || !(/^1[3456789]\d{9}$/.test(form.value.phone))) {
    showFailToast('手机号格式错误')
    return
  }
  if(!form.value.code || form.value.code.length !== 6) {
    showFailToast('验证码错误')
    return
  }
  submitLoading.value = true
  userStore.login({mobile: form.value.phone, code: form.value.code}).then((response) => {
    showSuccessToast('登录成功')
    const machineUrl = getSessionStorageItem("machine-url");
    if (!!machineUrl){
     window.location.href = machineUrl;
     return;
    }
    router.replace({path: "/index"})
  }).finally(() => submitLoading.value = false)
}

const init = () => {
  const countdownTimeFromSessionStorage = sessionStorage.getItem('countdownTime')
  let countdownTimeTotal = 0
  if (!!countdownTimeFromSessionStorage) {
    countdownTimeTotal = parseInt(countdownTimeFromSessionStorage)
  }
  if (!isNaN(countdownTimeTotal) && countdownTimeTotal > 0) {
    countdownTime.value = countdownTimeTotal
    startCountdown()
  }
}
init()
</script>

<template>
  <header>
    <div class="header">加入驾多宝，工资涨不停</div>
  </header>
  <van-form @submit="onSubmit" class="form">
    <div class="van-hairline--top"></div>
    <van-cell-group>
      <van-field
          :label-width="'90px'"
          size="large"
          class="input-field"
          center
          v-model="form.phone"
          name="手机号"
          placeholder="请输入手机号码"
      >
        <template #label>
          <div class="input-label">手机号：</div>
        </template>
      </van-field>
    </van-cell-group>
    <div class="van-hairline--top"></div>
    <van-cell-group>
      <van-field
          :label-width="'90px'"
          size="large"
          maxlength="6"
          center
          class="input-field"
          v-model="form.code"
          name="验证码"
          placeholder="请填写验证码"
      >
        <template #label>
          <div class="input-label">验证码：</div>
        </template>
        <template #button>
          <van-button
              :disabled="sendCodeBtnDisabled"
              type="primary"
              round
              @click.stop.prevent="getCode"
              color="#04be02"
              class="send-code-btn"
          >
            {{sendCodeBtnTxt}}
          </van-button>
        </template>
      </van-field>
    </van-cell-group>
    <div class="van-hairline--top"></div>
    <van-button
        class="submit-btn"
        block
        type="primary"
        native-type="submit"
        color="#04be02"
        :loading="submitLoading"
        loading-text="加载中..."
    >
      登 录
    </van-button>
  </van-form>
</template>

<style scoped>
.header {
  text-align: center;
  font-size: 19px;
  color: #3cc51f;
  font-weight: 400;
  margin: 50px auto;
}
:deep(.van-field__control::-webkit-input-placeholder) {
  color: #777;
  font-size: 16px;
}
:deep(.van-field__control::placeholder) {
  color: #777;
  font-size: 16px;
}
:deep(.van-field) {
  padding-left:0;
  padding-right: 0;
}
.form-cell-group :deep(.van-cell) {
  padding-left: 0;
  padding-right: 0;
  margin: 0 auto;
  height: 60px;
}
.send-code-btn {
  padding:0;font-size:14px;width:130px;height:35px;border-radius: 5px;margin-right:20px;
}
.submit-btn {
  margin:80px auto 0;font-size:18px;border-radius: 5px;width:330px
}
.input-label {
  width: 100%;text-align: right;color:#32434C;font-size:16px;
}
.input-field {
  background: none;font-size:16px;
}
</style>
